﻿using Shell32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BwTMusicServices;
using BwTMusicModels;

namespace AddMusicInfo
{
    public partial class Form1 : Form
    {
        MusicServices services = new MusicServices();
        public Form1()
        {
            InitializeComponent();
            dataGridView(musiclist);
            GetCountryList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog files = new OpenFileDialog();
            byte[] b = new byte[128];

            files.Multiselect = true;
            List<music> lists = new List<music>();
            //判断用户是否正确的选择了文件
            if (files.ShowDialog() == DialogResult.OK)
            {
                string[] items = files.FileNames;
                for (int i = 0; i < items.Length; i++)
                {
                    music li = new music();
                    ShellClass sh = new ShellClass();

                    Folder dir = sh.NameSpace(Path.GetDirectoryName(items[i]));

                    FolderItem item = dir.ParseName(Path.GetFileName(items[i]));

                    li.MusicName = dir.GetDetailsOf(item, 21);
                    li.SingerName = dir.GetDetailsOf(item, 13);//歌手
                    var longtime = dir.GetDetailsOf(item, 27);
                    li.MusicTime = longtime;
                    li.MusicLong = longtime.Remove(0, 3);
                    li.MusicBitRate = dir.GetDetailsOf(item, 28);
                    li.MusicAlbum = dir.GetDetailsOf(item, 14);
                    li.MusicUrl = items[i];
                    
                    TagLib.File pic = TagLib.File.Create(items[i]);
                    if (pic.Tag.Pictures.Length >= 1)
                    {
                        byte[] bin = pic.Tag.Pictures[0].Data.Data;
                        string url = @"d:\imgs\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
                        File.WriteAllBytes(url, bin);
                        li.MusicImgUrl = url;

                    }
                    lists.Add(li);

                }
                musiclist.DataSource = lists;
            }




        }

        private void dataGridView(DataGridView dataGridView)
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
            dataGridView.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            dataGridView.BackgroundColor = System.Drawing.Color.White;
            dataGridView.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            dataGridView.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
            dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
            dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView.EnableHeadersVisualStyles = false;
            dataGridView.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
            dataGridView.ReadOnly = true;
            dataGridView.RowHeadersVisible = false;
            dataGridView.RowTemplate.Height = 50;
            dataGridView.RowTemplate.ReadOnly = true;
            dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int singId = 0;
            int albumId = 0;
            try
            {
                var infos = (List<music>)musiclist.DataSource;
                List<North_Music> lists = new List<North_Music>();
                for (int i = 0; i < infos.Count; i++)
                {
                    string name = infos[i].SingerName;
                    string album = infos[i].MusicAlbum;
                    var falg = services.IsExistSinger(name);

                    if (!falg)
                    {
                        North_Singer singer = new North_Singer();
                        singer.SingerName = infos[i].SingerName;
                        singer.SingerCountry = comboBox1.SelectedValue.ToString();
                        var reslut = services.AddMusicSinger(singer);
                        singId = services.GetSingerId(singer.SingerName).SingerId;
                    }
                    else
                    {
                        singId = services.GetSingerId(name).SingerId;
                    }

                    if (!string.IsNullOrEmpty(album.Trim()))
                    {
                        var falg2 = services.IsExistAlbum(infos[i].MusicAlbum);
                        if (!falg2)
                        {
                            North_Album albums = new North_Album();
                            albums.AlbumName = infos[i].MusicAlbum;
                            var reslut = services.AddAAlbum(albums);
                            albumId = services.GetAlbumId(albums.AlbumName).AlbumId;
                        }
                        else
                        {
                            albumId = services.GetAlbumId(album).AlbumId;
                        }
                    }
                    else
                    {
                        albumId = -1;
                    }

                    string AlbumStr = null;
                    if (albumId != -1)
                    {
                        AlbumStr = albumId.ToString();
                    }                  
                    North_Music musics = new North_Music()
                    {
                        MusicId = infos[i].MusicId,
                        MusicAlbum= AlbumStr,
                        MusicBitRate = infos[i].MusicBitRate.ToString(),
                        MusicCategory = comboBox2.SelectedValue.ToString(),
                        MusicImgUrl = infos[i].MusicImgUrl,
                        MusicLong = infos[i].MusicLong,
                        MusicName = infos[i].MusicName,
                        MusicSinger = singId,
                        MusicTag = comboBox3.SelectedValue.ToString(),
                        MusicTime = infos[i].MusicTime,
                        MusicUrl = infos[i].MusicUrl
                    };
                    lists.Add(musics);

                    North_Music m = new North_Music();
                    m = lists[i];
                    var result = services.AddMusicInfo(m);
                    SingerMusic singerMusic = new SingerMusic();
                    singerMusic.MusicId = services.GetMusicId(m.MusicName).MusicId;
                    singerMusic.SingerId = singId;
                    var addSingerMusic = services.AddSingerMusic(singerMusic);
                    if (m.MusicAlbum != null)
                    {
                        SingerAlbum album1= new SingerAlbum();
                        album1.SingerId = singId;
                        album1.AlbumId = albumId;
                        var addSingerAlbum = services.AddSingerAlbum(album1);
                    }
                }               
                musiclist = null;

                MessageBox.Show("添加完成,请继续上传");
            }
            catch (Exception ex)
            {

            }
          
        }
        public void GetCountryList()
        {
            List<North_Country> lists = new List<North_Country>();
            lists = services.GetAllCountrys();
            comboBox1.DataSource = lists;
            comboBox1.ValueMember = "CountryId";
            comboBox1.DisplayMember = "CountryName";

            List<North_Genre> genre = new List<North_Genre>();
            genre = services.GetAllGenres();
            comboBox3.DataSource = genre;
            comboBox3.DisplayMember = "GenreName";
            comboBox3.ValueMember = "GenreId";

            List<North_Theme> theme = new List<North_Theme>();
            theme = services.GetAllThemes();
            comboBox2.DataSource = theme;
            comboBox2.DisplayMember = "ThemeName";
            comboBox2.ValueMember = "ThemeId";
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
